package de.tudarmstadt.ukp.muip.fibqdojo.japanese;

import java.util.LinkedList;
import java.util.List;

import com.google.common.base.Optional;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQFilter;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQRule;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetSentenceTokenListVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.SetSimplifyTokenVisitor;

/**
 * This filter simplifies japanese tokens. If one of the provided rules
 * indicates <i>filtering</i> the respective token is (e.g. replaced by an
 * alternative form if available)
 * 
 * @author Tobias Horsmann
 */
public class JapaneseTokenSimplifyFilter implements IFIBQFilter {

    private final List<IFIBQRule> filterRules = new LinkedList<IFIBQRule>();

    public JapaneseTokenSimplifyFilter(IFIBQRule... filterRules) {
        for (IFIBQRule rule : filterRules) {
            this.filterRules.add(rule);
        }
    }

    @Override
    public Optional<IFIBQSentence> apply(IFIBQSentence sentence) {

        for (IFIBQSentenceToken token : getSentenceToken(sentence)) {
            if (simplify(token)) {
                SetSimplifyTokenVisitor setSimplify = new SetSimplifyTokenVisitor();
                token.accept(setSimplify);
            }
        }
        return Optional.of(sentence);
    }

    private List<IFIBQSentenceToken> getSentenceToken(IFIBQSentence sentenceCopy) {
        GetSentenceTokenListVisitor sentTokenList = new GetSentenceTokenListVisitor();
        sentenceCopy.accept(sentTokenList);
        return sentTokenList.getSentenceTokens();
    }

    private boolean simplify(IFIBQSentenceToken token) {
        for (IFIBQRule rule : filterRules) {
            if (rule.match(token)) {
                return true;
            }
        }
        return false;
    }

}
